/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import dao.ListCategory;
import dao.ListSearchVacancy;
import dao.ListWorkPlace;
import dao.loginDao;
import entities.Category;
import entities.SearchVacancy;
import entities.WorkPlace;
import entities.userName;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author hungxp
 */
@WebServlet(name = "loginServlets", urlPatterns = {"/loginServlets"})
public class loginServlets extends HttpServlet {

    loginDao lgd = new loginDao();
    userName use = null;
    ListCategory daoCate = new ListCategory();
    ListWorkPlace daoWP = new ListWorkPlace();
    ListSearchVacancy daoVaHot = new ListSearchVacancy();

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Category> listC = daoCate.getListCategory();
        List<WorkPlace> listWP = daoWP.getListWorkPlace();
        List<SearchVacancy> lisva = daoVaHot.getlistHot();
        request.setAttribute("listC", listC);
        request.setAttribute("listWP", listWP);
        request.setAttribute("lisVa", lisva);
        request.getRequestDispatcher("user/login.jsp").forward(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        String name = request.getParameter("user");
        String pass = request.getParameter("pass");
        use = lgd.checkLogin(name, pass);
        if (use != null) {
            if (use.getRoles().equals("customer")) {
                session.setAttribute("username", use);
                List<Category> listC = daoCate.getListCategory();
                List<WorkPlace> listWP = daoWP.getListWorkPlace();
                List<SearchVacancy> lisva = daoVaHot.getlistHot();
                request.setAttribute("listC", listC);
                request.setAttribute("listWP", listWP);
                request.setAttribute("lisVa", lisva);
                request.getRequestDispatcher("user/home.jsp").forward(request, response);
            } else if (use.getRoles().equals("admin")) {
                request.getRequestDispatcher("admin/homeAdmin.jsp").forward(request, response);
            }
        } else {
            String eror = null;
            if (name == "" & pass == "") {
                eror = "The field is not empty!";
            } else {
                eror = "Account does not exist!";
            }
            List<Category> listC = daoCate.getListCategory();
            List<WorkPlace> listWP = daoWP.getListWorkPlace();
            List<SearchVacancy> lisva = daoVaHot.getlistHot();
            request.setAttribute("listC", listC);
            request.setAttribute("listWP", listWP);
            request.setAttribute("lisVa", lisva);
            request.setAttribute("eror", eror);
            request.getRequestDispatcher("user/login.jsp").forward(request, response);
        }

    }

    
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
